package myJs.implicits

import scala.scalajs.js
import scala.scalajs.js.{Any, Date, JSON, URIUtils, native}
import myJs.Implicits._

import scala.util.Try

/**
 * Created by Administrator on 2020/1/16
 */
trait MyStringTool {

  type JArJDJAn = js.Array[js.Dictionary[js.Any]]
  type JArJDS = js.Array[js.Dictionary[String]]
  type JDJAn = js.Dictionary[js.Any]
  type JDS = js.Dictionary[String]
  type JArS = js.Array[String]

  implicit class MyString(v: String) {

    def toJDS = {
      JSON.parse(v).toJDS
    }

    def toJDI = {
      JSON.parse(v).toJDI
    }

    def toJDB = {
      JSON.parse(v).toJDB
    }

    def toJDJAn = {
      JSON.parse(v).toJDJAn
    }

    def toJArJDS = {
      JSON.parse(v).toJArJDS
    }

    def toJArS = {
      JSON.parse(v).toJArS
    }

    def toJArI = {
      JSON.parse(v).toJArI
    }

    def toJArJDJAn = {
      JSON.parse(v).toJArJDJAn
    }

    def headLower = {
      if (v == null || v.length == 0 || !v.charAt(0).isUpper) v
      else v.updated(0, v.charAt(0).toLower)
    }

    def nonBlank = {
      v.trim.nonEmpty
    }

    def strIsBlank = {
      !nonBlank
    }

    def px2Int = {
      v.replaceAll("px$", "").toInt
    }

    def px2Double = {
      v.replaceAll("px$", "").toDouble
    }

    def fileNamePrefix: String = {
      val index = v.lastIndexOf(".")
      v.substring(0, index)
    }

    def containsIgnoreCase(elem: String) = {
      v.toLowerCase.contains(elem.toLowerCase)
    }

    def toMyArray = {
      if (v.isEmpty) {
        js.Array[String]()
      } else js.Array(v)
    }

    def noCache = {
      s"${v}?time=${new Date().getTime()}"
    }

    def encodeURI={
      URIUtils.encodeURIComponent(v)
    }

    def isBlank = {
      !nonBlank
    }

    def isDouble: Boolean = {
      Try(v.toDouble).toOption match {
        case Some(value) => true
        case None => false
      }
    }

    def toDoubleO: Boolean = {
      Try(v.toDouble).toOption match {
        case Some(value) => true
        case None => false
      }
    }


  }

}
